/**
 * Created by dale on 2017/8/9.
 */
var list= $('#List'),
    select_date =$('#select_date'),
    startDom    = $('#List_Search_StartTime'),
    endDom      = $('#List_Search_EndTime');

function Trim(str){
  return str.toString().replace(/^,*|,*$/g,'');
}
$(document).ready(function () {
    $("#select_nodes").combobox({
        onChange: function(n,o) {
            var nn=Trim(n);
            $(this).combobox('setValues',nn);
        }
    });

    startDom.datebox();
    endDom.datebox();

    setWeek();

    $('.xh-datagrid-toolbar').parseToolbar();
    xhTool.bindDocKeyDown($('#bt_list_search'));


    var params=getParams();
    list.initDataGrid(modelUrl+'plan/getPlanRoute',{queryParams:params});
    DataGrid.onLoadSuccess = function(data){
        if (data.rows.length > 0) {
            list.mergeCellsByField(['tStartTime']);
            list.mergeCellsByField(['sSchoolName']);
        }
    };

    $('#bt_list_search').click(function () {
        var params = getParams();
        var dateType = xhExtra.getValue($('#select_date'));
        if(dateType=='tPlanDate'){
            list.datagrid({
                    url:modelUrl+'plan/getPlanRoute',
                    queryParams:params,
                        columns:[[
                            {field:'iBPCId',title:'iBPCId',width:100,hidden:true},
                            {field:'tPlanDate',title:'预计完成时间',width:120,align:'center',halign:'center'},
                            {field:'sSchoolName',title:'学校名称',width:220,align:'left',halign:'center'},
                            {field:'sNodeName',title:'节点名称',width:120,align:'left',halign:'center'},
                            {field:'tStartTime',title:'发起时间',width:120,align:'center',halign:'center'},
                            {field:'tRealDate',title:'实际完成时间',width:120,align:'center',halign:'center'},
                            {field:'sChargeName',title:'负责人',width:220,align:'left',halign:'center'},
                        ]],
                    onLoadSuccess: function(data){
                        if (data.rows.length > 0) {
                            list.mergeCellsByField(['tPlanDate']);
                            list.mergeCellsByField(['sSchoolName']);
                        }
                     },
            });
        }else if(dateType=='tRealDate'){
            list.datagrid({
                    url:modelUrl+'plan/getPlanRoute',
                    queryParams:params,
                        columns:[[
                            {field:'iBPCId',title:'iBPCId',width:100,hidden:true},
                            {field:'tRealDate',title:'实际完成时间',width:120,align:'center',halign:'center'},
                            {field:'sSchoolName',title:'学校名称',width:220,align:'left',halign:'center'},
                            {field:'sNodeName',title:'节点名称',width:120,align:'left',halign:'center'},
                            {field:'tStartTime',title:'发起时间',width:120,align:'center',halign:'center'},
                            {field:'tPlanDate',title:'预计完成时间',width:120,align:'center',halign:'center'},
                            {field:'sChargeName',title:'负责人',width:220,align:'left',halign:'center'},
                    ]],
                    onLoadSuccess: function(data){
                        if (data.rows.length > 0) {
                            list.mergeCellsByField(['tRealDate']);
                            list.mergeCellsByField(['sSchoolName']);
                        }
                    },
            });
        }else if(dateType=='tStartTime'){
            list.datagrid({
                    url:modelUrl+'plan/getPlanRoute',
                    queryParams:params,
                    columns:[[
                        {field:'iBPCId',title:'iBPCId',width:100,hidden:true},
                        {field:'tStartTime',title:'发起时间',width:120,align:'center',halign:'center'},
                        {field:'sSchoolName',title:'学校名称',width:220,align:'left',halign:'center'},
                        {field:'sNodeName',title:'节点名称',width:120,align:'left',halign:'center'},
                        {field:'tPlanDate',title:'预计完成时间',width:120,align:'center',halign:'center'},
                        {field:'tRealDate',title:'实际完成时间',width:120,align:'center',halign:'center'},
                        {field:'sChargeName',title:'负责人',width:220,align:'left',halign:'center'},
                    ]],
                    onLoadSuccess: function(data){
                        if (data.rows.length > 0) {
                            list.mergeCellsByField(['tStartTime']);
                            list.mergeCellsByField(['sSchoolName']);
                        }
                    },
        });
        }

    });

    $('#excel_export').click(function () {
        var startTime   = startDom.getElementValue(),
            endTime     = endDom.getElementValue();
            dateType    = xhExtra.getValue($('#select_date')),
            key         = xhExtra.getValue($('#list_search')),
            nodes       = xhExtra.getValue($('#select_nodes'))
        window.open(modelUrl+'plan/exportPlanRoute?dateType='+dateType+'&startTime='+startTime+'&endTime='+endTime+'&key='+key+'&nodes='+nodes);
    });


    
});

function setWeek() {
    var d=new Date().getDay();
    if(d==0){
        startDom.datebox('setValue', GetDateStr(-7));
        endDom.datebox('setValue', GetDateStr(0));
    }else{
        startDom.datebox('setValue', GetDateStr(-d+1));
        endDom.datebox('setValue', GetDateStr(7-d));
    }
}
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;//获取当前月份的日期
    var d = dd.getDate();
    return y+"-"+m+"-"+d;
}

function getParams() {
    var startTime   = startDom.getElementValue(),
        endTime     = endDom.getElementValue();
        dateType    = xhExtra.getValue($('#select_date')),
        key         = xhExtra.getValue($('#list_search')),
        nodes       = xhExtra.getValue($('#select_nodes'))
    return {dateType:dateType,startTime:startTime,endTime:endTime,key:key,nodes:nodes}
}